<?php

class HmEvent_HmEventController extends Fw_Main
{
	public function addAction()
	{
		$this->validateUser();
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
			if($formData) {
				$date_start = $this->_request->getPost('start_year').'-'.$this->_request->getPost('start_month').'-'.$this->_request->getPost('start_day').' '.$this->_request->getPost('start_hour').':'.$this->_request->getPost('start_minute').':'.$this->_request->getPost('start_second');
				$data = array(
							  'title'         => $this->_request->getPost('title'),
							  'description'   => $this->_request->getPost('description'),
							  'date_start'    => $date_start,
							  'date_register' => date('Y-m-d H:i:s'),
							  'active'        => $this->_request->getPost('active')
							  );
				$_model = $this->_getModel('HmEvent');
				$add_row = $_model->addRow($data);
				$this->createDir('HmEvent');
				$this->uploadFile('HmEvent');
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function editAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmEvent');
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
			if($formData) {
				$date_start = $this->_request->getPost('start_year').'-'.$this->_request->getPost('start_month').'-'.$this->_request->getPost('start_day').' '.$this->_request->getPost('start_hour').':'.$this->_request->getPost('start_minute').':'.$this->_request->getPost('start_second');
				$date_end   = $this->_request->getPost('end_year').'-'.$this->_request->getPost('end_month').'-'.$this->_request->getPost('end_day').' '.$this->_request->getPost('end_hour').':'.$this->_request->getPost('end_minute').':'.$this->_request->getPost('end_second');

				$data = array(
							  'title'         => $this->_request->getPost('title'),
							  'description'   => $this->_request->getPost('description'),
							  'date_start'    => $date_start,
							  'date_end'      => $date_end,
							  'active'        => $this->_request->getPost('active')
							  );
				$update_row = $_model->updateRow($id, $data);
				return $this->_helper->redirector('list');
			}
		}
		$edit_row = $_model->getRow($id);
		$this->view->edit_event = $edit_row;		
	}
	
	public function deleteAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmEvent');
		$delete_row = $_model->deleteRow($id);
		$this->removeDir('HmEvent', $id);
		return $this->_helper->redirector('list');
	}
	
	public function listAction()
	{
		$this->validateUser();
		$_model = $this->_getModel('HmEvent');
		$list_row = $_model->getAll();
		$this->view->list_event = $list_row;
		$this->view->rowcount = count($list_row);
	}
	
	public function nextAction()
	{
		$limit = (int)$this->_request->getParam('limit');
		$_model = $this->_getModel('HmEvent');
		$next_row = $_model->getNext($limit);
		$this->view->next_event = $next_row;
		$this->view->rowcount = count($next_row);
	}
	
	public function showAction()
	{
		if(FW_MODE == 'admin') { $this->validateUser(); }
		// $this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmEvent');
		$show_row = $_model->getRow($id);
		$this->view->show_event = $show_row;
	}
	
	public function tagsAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		
		$this->view->rowid = $id;
		
		$_model = $this->_getModel('HmEventTagsDescr');
		$list_tags = $_model->getAll();
		$this->view->list_tags = $list_tags;
		$this->view->count_tags = count($list_tags);
		
		$_newModel = $this->_getNewModel('HmEventTags');
		$link_tags = $_newModel->getLink($id);
		$this->view->link_tags = $link_tags;
		$this->view->count_tlink = count($link_tags);
	}
	
	public function tagsSetAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmEventTags');
		$data = array('hm_event_id' => $id, 'hm_event_tags_descr_id' => $this->_request->getParam('tags_id'));
		$add_row = $_model->addRow($data);
		
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->_redirector->gotoSimple('tags','hm-event','hm-event',array('id' => $id));
		return;
	}
	
	public function tagsAddSetAction()
	{
		$this->validateUser();
		if($this->_request->isPost()){
			$formData = $this->_request->getPost('add');
			if($formData) {
				$data = array('description' => $this->_request->getPost('description'));
				$_newModel = $this->_getNewModel('HmEventTagsDescr');
				$add_row = $_newModel->addRow($data);
			}
			
			$list_row = $_newModel->getLast('id',1);
			$id = (int)$this->_request->getParam('id');
			foreach($list_row as $key) {
				$tags_id = $key['id'];
			}

			$_model = $this->_getModel('HmEventTags');
			$data = array('hm_event_id' => $id, 'hm_event_tags_descr_id' => $tags_id);
			$add_row = $_model->addRow($data);
			
			$this->_redirector = $this->_helper->getHelper('Redirector');
			$this->_redirector->gotoSimple('tags','hm-event','hm-event',array('id' => $id));
			return;
		}
	}
	
	public function tagsUnsetAction()
	{
		
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$article_tags = (int)$this->_request->getParam('tags_id');
		$_model = $this->_getModel('HmEventTags');
		$delete_tags = $_model->deleteRow($id, $article_tags);
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->_redirector->gotoSimple('tags','hm-event','hm-event',array('id' => $id));
		return;
	}

}